/*
 * @Copyright    : Blueray Med .ltd
 * @file name    : sys_handler.c
 * @Date         : 2022-10-19 16:09:05
 * @LastEditor   : Liam
 * @Description  : 系统任务的处理：切换页面；音量开关；调节亮度等。
 */
#include "sys_handler.h"

u8 picset_cmd[4]; //设置跳转界面的函数数组
u8 open_sound_cmd[4] = {0x5A, 0x00, 0x00, 0x38};//180°旋转屏幕3A--   0°38   90°39    270° 3B
u8 close_sound_cmd[4] = {0x5A, 0x00, 0x00, 0x30};//180°旋转屏幕32--  0°30   90°31    270° 33
u8 nor_flash_cmd[8];

/**
 * @Function: pic_set
 * @Description: 切换相应的页面函数
 * @Author: Liam
 * @Input: 16进制的页面数字
 * @Return: null
 * @Others: note
 * @result: null
 */
void pic_set(u8 page_num) //顺序是反着的0对应着最高位
{
	picset_cmd[0] = 0x5A;
	picset_cmd[1] = 0x01;
	picset_cmd[2] = 0x00;
	picset_cmd[3] = page_num;
	sys_write_vp(PLC_SET_ADDR, (u8 *)&picset_cmd, 2);
}
/**
 * @Function: sys_the_sound
 * @Description: 打开与关闭按键提示声音
 * @Author: Liam
 * @Input: bit类型 已进行宏定义open为1 close为0
 * @Return: null
 * @Others: note
 * @result: 结果
 */
void sys_the_sound (u8 open_or_close)
{
	if (open_or_close)
	{
		sys_write_vp(SYSTEM_CONFIG_ADDR, (u8 *)&open_sound_cmd, 2); //打开按键的声音
	}
	else
	{
		sys_write_vp(SYSTEM_CONFIG_ADDR, (u8 *)&close_sound_cmd, 2); //关闭按键的声音
	}
}

/**
 * @Function: nor_flash_rw_cmd
 * @Description: 将系统变量写入或读外部flash
 * @Author: Liam
 * @Input: 16进制的页面数字
 * @Return: null
 * @Others: note
 * @result: null
 */
void nor_flash_rw_cmd(u8 w_or_r, u32 flash_addr, u16 system_addr, u16 len)
{
	nor_flash_cmd[0] = w_or_r;
	nor_flash_cmd[1] = flash_addr>>16;
	nor_flash_cmd[2] = flash_addr>>8;
	nor_flash_cmd[3] = flash_addr;
	nor_flash_cmd[4] = system_addr>>8;
	nor_flash_cmd[5] = system_addr;
	nor_flash_cmd[6] = len>>8;
	nor_flash_cmd[7] = len;
	sys_write_vp(NOR_FLASH_ADDR, (u8 *)&nor_flash_cmd, 4);
}

/**
 * @Function: write_VP_check
 * @Description: 数据写入结构体并校验
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void write_VP_check (u16 write_addr,u16 write_value)
{
    u16 read_value;
    do
    {
        sys_write_vp(write_addr, (u8 *)&write_value, 1); /* 将设定的值写入进相应的地址 */
        sys_read_vp(write_addr, (u8 *)&read_value, 1);   /* 将相应地址的值读出 判断是否为写入的值 */
    } while (read_value != write_value);
}

/**
 * @Function: language_choose
 * @Description: 
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void language_choose(u16 language_value)
{
#define CHINESE_ICL 57
#define ENGLISH_ICL 113
    static u16 last_value;
    if (last_value != language_value)
    {
        last_value = language_value;
        if (language_value == 1) /* 英文选择 */
        {
            write_VP_check(LANGUAGE_SET_ADDR, ENGLISH_ICL);
        }
        else if (language_value == 0) /* 中文选择 */
        {
            write_VP_check(LANGUAGE_SET_ADDR, CHINESE_ICL);
        }
    }
}

/**
 * @Function: the_language_enable
 * @Description: 语言设置使能
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void the_language_enable()
{
#define LANGUAGE_ENABLE 0x5A00
    u16 ii;
    write_VP_check(LANGUAGE_CONFIG, LANGUAGE_ENABLE);
    nor_flash_rw_cmd(NOR_FLASH_R, 0x00000002, LANGUAGE_MODE, 0x0008); /* 读取型号 */
    sys_delay_about_ms(240);
    sys_read_vp(LANGUAGE_MODE, (u8 *)&ii, 1);                         /* 读取按键值 */
    language_choose(ii);                                              /* 将语言mode写入 */
    pic_set(PIC_NUM_OPEN);
}
